package cn.itcast.controller;

import cn.itcast.domain.Dept;
import cn.itcast.domain.User;
import cn.itcast.service.DeptService;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.DeptServiceImpl;
import cn.itcast.service.impl.UserServiceImpl;
import cn.itcast.utils.BeanUtil;
import cn.itcast.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    private DeptService deptService = new DeptServiceImpl();

    // TODO: 2020年7月23日09:03:40   ********  查询所有功能  *********
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前页

        String page = req.getParameter("page");
        int pageNum = 1;
        if (StringUtils.isNoneBlank(page)) {
            pageNum = Integer.parseInt(page);
        }
        List<User> userList = userService.findAll(pageNum);
        PageInfo<User> pageInfo = new PageInfo<User>(userList);

        req.setAttribute("page", pageInfo);

        req.getRequestDispatcher("/pages/user/list.jsp").forward(req, resp);

    }

    // TODO: 2020年7月23日09:04:04   ********  跳转新增页面功能  *********
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("哈哈");
        List<Dept> deptList = deptService.findAll();

        req.setAttribute("deptList", deptList);
        req.getRequestDispatcher("/pages/user/add.jsp").forward(req, resp);
    }

    // TODO: 2020年7月23日09:07:30   ********  新增功能 //有bug *********
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");

        userService.add(user);
        resp.sendRedirect(req.getContextPath() + "/store/user/findAll");
    }

    // TODO: 2020年7月23日19:56:50   ********  数据回显功能  *********
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");
        System.out.println(id + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        User u = userService.findById(id);
        ;
        System.out.println(u);
        req.setAttribute("user", u);
        req.getRequestDispatcher("/pages/user/update.jsp").forward(req, resp);
    }

    // TODO: 2020年7月23日20:23:12   ********  修改  *********
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = BeanUtil.fillBean(req, User.class, "yyyy-MM-dd");

        userService.update(user);

        resp.sendRedirect(req.getContextPath() + "/store/user/findAll");
    }

    // TODO: 时间   ********  批量删除功能  *********
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");

        userService.deleteByIds(ids);


        resp.sendRedirect(req.getContextPath() + "/store/user/findAll");
    }

}
